import api from "../../common/api";
import {setData} from "../../models/transferData";
import { gioCooperationLog,gioBaseLog } from "../../common/gioHandler"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list_item:[],
    cooperationId:'', //房源id
    toUserId:'',//对方的用户id
    isVmShow:false,//控制打电话弹窗的显示
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    getApp().globalData.loginPromise.then(() => {
      this.setData({
        cooperationId:options.cooperationId,
      })
      gioBaseLog('visitor_page_view');
      this.houseInfoList(options.cooperationId)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    setData("isRefreshCooperation", true);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  // 去聊天
  gotoLine(e){
    getApp().globalData.loginPromise.then(({ sessionUser }) => {
      gioCooperationLog('IM_click', sessionUser.id, '房源访客页');
    })
    const unionid = e.currentTarget.dataset.unionid
    const cooperationId = this.data.cooperationId
    wx.navigateTo({
      url: `/pages/im/imMessagelist/imMessagelist?unionid=${unionid}&cooperationId=${cooperationId}&fromInfo=visitorList&pageFrom=房源访客页`,
    })
  },

  // 打电话
  gotoPhone(e){
    getApp().globalData.loginPromise.then(({ sessionUser }) => {
      gioCooperationLog('cooperation_phone_click', sessionUser.id, '房源访客页');
    })
    this.setData({
      isVmShow:true,
      toUserId:e.currentTarget.dataset.userid,
      cooperationId:'FK_'+ this.data.cooperationId
    })
  },
  handleVmVisible(e) {
    this.setData({
      isVmShow: e.detail.visible
    });
  },

  // 获取数据
  houseInfoList(cooperationId){
    this.selectComponent("#com-scroll-content").fetchHouse(1, ({page}) => {
      return new Promise((resolve, reject) => {
        api.searchMyCooperationVisitor({page,pageSize: 10,cooperationId}, ({data}) => {
          data.handleData = (rows) => {
            let tmpRow = []
            for (let i in rows) {
              let tmpRowItem = {}
              tmpRowItem.avatar = rows[i].avatar
              tmpRowItem.realName= rows[i].realName ||rows[i].nickname
              // 计算联系方式
              if(rows[i].haveCommunicateType=='IM'){
                tmpRowItem.haveCommunicateType = '在线联系过'
              }
              if(rows[i].haveCommunicateType=='PHONE'){
                tmpRowItem.haveCommunicateType = '电话联系过'
              }
              if(rows[i].haveCommunicateType=='BOTH'){
                tmpRowItem.haveCommunicateType = '在线、电话联系过'
              }
               // 计算浏览时间
               let newTime = new Date().getTime()
               let LoginDate = rows[i].LoginDate = (newTime - rows[i].lastLoginDate) / 1000 //单位秒
               if(LoginDate <= 60){
                 tmpRowItem.lookTime = '刚刚浏览'
               }
               if(60 < LoginDate && LoginDate < 3600){
                 tmpRowItem.lookTime = parseInt(LoginDate/60) +"分钟前浏览"
               }
               if(3600 <= LoginDate && LoginDate < 86400){
                 tmpRowItem.lookTime = parseInt(LoginDate/3600) +"小时前浏览"
               }
               if(86400 <= LoginDate && LoginDate < 2678400){
                 tmpRowItem.lookTime = parseInt(LoginDate/86400) +"天前浏览"
               }
               if(2678400 <= LoginDate){
                 tmpRowItem.lookTime = "一个月前浏览"
               }
              tmpRowItem.viewCount= rows[i].viewCount
              tmpRowItem.store= rows[i].store
              tmpRowItem.userId= rows[i].userId
              tmpRowItem.unionId= rows[i].unionId
              tmpRow.push(tmpRowItem)
            }
            this.setData({
              list_item: this.data.list_item.concat(tmpRow),
            });
          };
          resolve(data);
        }, reject);
      });
    })    
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})